package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constants.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
* checkItem controller
* */
@RestController
@RequestMapping("/checkItem")
public class CheckItemController {


    @Reference
    CheckItemService checkItemService;

    /*
    * 分页查询检查项目
    * */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        if (queryString != null) {
            // 条件非空时,模糊检索添加"%"
            queryString = "%" + queryString + "%";
        }

        try {
            PageResult pageResult = checkItemService.findPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryString);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll() {

        try {
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /*
    * 添加检查项
    * */
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    public Result add(@RequestBody CheckItem checkItem) {

        try {
            checkItemService.add(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    /*
    * 添加检查项目时先查询总数目
    * */
    @RequestMapping("/totalCheckItem")
    public Result totalCheckItem() {
        try {
            Integer integer = checkItemService.totalCheckItem();
            return new Result(true,null,integer);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /*
    * 删除检查项
    * */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deleteItem")
    public Result deleteItem(Integer id) {
        try {
            checkItemService.deleteItem(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }


    /*
    * 编辑检查项前获取数据
    * */
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findByItemId(id);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS);
        }
    }



    /*
     * 检查项更新(编辑检查项提交)
     * */
    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/updateItem")
    public Result updateItem(@RequestBody CheckItem checkItem) {

        try {
            checkItemService.updateItem(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true,MessageConstant.EDIT_CHECKITEM_FAIL);
        }


    }
}
